import Vue from 'vue'
import Vuex from 'vuex'
import { storageGet, storageRemove, storageSet } from '@/appBase/zkStorage';
import { loadLanguageAsync } from '@/locales'
// import { queryUserTodoCount } from '@/api/todo';
Vue.use(Vuex)

const store = new Vuex.Store({
    //状态
    state: {
        loadding: false,//加载
        // /** 是否显示全局加载窗 */
        hasGlobalLoading: true,
        /** 动态路由 */
        dynamicRouter: null,
        /** 当前登录的用户信息 */
        nowUserInfo: {
            hasVal: false,
            id: null,
            name: null,
            userType: null,
            /** 权限列表  "*:*:*" */
            permissions:[],
            /** 角色列表 */
            roles:[],
            userId: 0,
            deptId: 0,
            userName: "",
            nickName: "",
            email: "",
            phonenumber: "",
            sex: "",
            avatar: "",
            salt: null,
            status: "",
            delFlag: "",
            loginIp: "",
            loginDate: "",
            dept: {
                params: {},
                deptId: 0,
                parentId: 0,
                ancestors: null,
                deptName: "",
                orderNum: "",
                leader: "",
                phone: null,
                email: null,
                status: "0",
                delFlag: null,
                parentName: null,
                children: []
            },
            roles: [
                // {
                //     "params": {},
                //     "roleId": 1,
                //     "roleName": "超级管理员",
                //     "roleKey": "admin",
                //     "roleSort": "1",
                //     "dataScope": "1",
                //     "menuCheckStrictly": false,
                //     "deptCheckStrictly": false,
                //     "status": "0",
                //     "delFlag": null,
                //     "flag": false,
                //     "menuIds": null,
                //     "deptIds": null,
                //     "admin": true
                // }
            ],
            roleIds: null,
            postIds: null,
            roleId: null,
            admin: true,
            // 定制用户信息
            /** 当前部门信息 */
            deptName: "",
            /** M 职级 */
            mLevel: null,
            /** P 职级 */
            pLevel: null,
            /** 入职时间 */
            entryTime: null, 
        },
        /** 用户 token */
        token: null,
        /** routerView显示状态 */
        routerViewState: true,
        /** 待办通知数量 */
        userTodoCount:{
            // 未读消息数量
            msgCount: 0,
            // 待办数量
            todoCount: 0
        },
        lang: 'en-US',
    },
    //状态修改方法
    // 使用store.commit()来调用
    mutations: {
        setAppLang(state, lang) {
            state.lang = lang
        },
        // loadding
        showloadding(state, load) {
            state.loadding = load
        },
        CloseGlobalLoading(state) {
            state.hasGlobalLoading = false;
        },
        EnableGlobalLoading(state) {
            state.hasGlobalLoading = true;
        },
        UpdateDynamicRouter(state, params) {
            state.dynamicRouter = params;
        },
        // 更新当前用户信息
        UpdateNowUserInfo(state, val){
            Object.assign(state.nowUserInfo,val);
            sessionStorage.setItem('ao_userinfo',JSON.stringify(state.nowUserInfo));
        },
        // 更新当前用户信息
        UpdateToken(state, val){
            state.token = val;
            if (val==null) {
                storageRemove('access_token');
                state.token = null;
                return;
            }
            storageSet('access_token',val, 24 * 60 * 60 * 1000);
        },
        /** 清空当前用户信息 */
        ClearNowUserInfo(state, val){
            Object.keys(state.nowUserInfo).forEach(key=>{
                state.nowUserInfo[key]=null;
            });
            sessionStorage.removeItem('ao_userinfo');
        },
        UpdateRouterViewState(state, val){
            state.routerViewState = val;
        },
        UpdateUserTodoCount(state, val){
            Object.assign(state.userTodoCount,val);
        }
    },
    //调用mutations，可以包含异步操作
    // 可以用store.dispatch来调用
    actions: {
        // setloadding(context, load) {
        //     context.commit("showloadding", load);
        // },
        // 刷新用户待办数量
        async refreshUserTodoCount({ commit }){
            // const res = await queryUserTodoCount();
            commit("UpdateUserTodoCount", []);
        },
        setLang ({ commit }, lang) {
            return new Promise((resolve, reject) => {
              commit('setAppLang', lang)
              loadLanguageAsync(lang).then(() => {
                resolve()
              }).catch((e) => {
                reject(e)
              })
            })
          }
    },
    getters: {
        isloading: (state) => {
            return state.loadding
        },
        multiTab:(state)=>{
            return true;
        }
    }
});
function init() {
    const info = sessionStorage.getItem('ao_userinfo');
    const token = storageGet('access_token');
    if (info) {
        store.state.nowUserInfo = JSON.parse(info);
    }
    if (token) {
        store.state.token = token;
    }
}
init();
// 导出!!!!
export default store